package client

import (
	"fmt"
	"github.com/sirupsen/logrus"
	"github.com/urfave/cli/v2"
	"ycontainer/network"
)

func NetworkCreateAction(context *cli.Context) error {
	if context.Args().Len() < 1 {
		return fmt.Errorf("missing netwoek name")
	}

	network.InitDriverAndNetwork()

	driver := context.String("driver")
	subnet := context.String("subnet")
	if err := network.CreateNetwork(driver, subnet, context.Args().First()); err != nil {
		logrus.Errorf("create network err: %v", err)
	}
	return nil
}

func NetworkDeleteAction(context *cli.Context) error {
	if context.Args().Len() < 1 {
		return fmt.Errorf("missing network name")
	}

	network.InitDriverAndNetwork()

	if err := network.DeleteNetwork(context.Args().Slice()); err != nil {
		logrus.Errorf("create network err: %v", err)
	}
	return nil
}

func NetworkListAction(context *cli.Context) error {
	network.InitDriverAndNetwork()
	network.ListNetwork()
	return nil
}
